Consider the translation scheme shown belowS → T RR → + T ...
Let us make the parse tree for 9+5+2 in top down manner, left first derivation.
Steps:
1) Exapnd S->TR
2) apply T->Num...
3) apply R -> +T...
4) appy T->Num...
5) apply R-> +T..
6) apply T-> Num..
7) apply R-> epsilon
After printing through the print statement in the parse tree formed you will get the answer as 95+2+
View all questions of this test
Consider the translation scheme shown belowS → T RR → + T ...
Explanation:
This translation scheme is used to translate an input string into a sequence of print statements. The input string consists of a series of numbers separated by whitespace.
The translation scheme consists of two production rules:
1. S → T R
2. R → T {print ();} R | ε
3. T → num {print (num.val);}
Let's break down the given input string and see how it is translated.
Input string: 9 5 2
S → T R:
- Apply production rule 1, S → T R, which expands the start symbol S into T R.
- T is the non-terminal symbol that represents a number and R is the non-terminal symbol that represents the remaining part of the input string.
- Now we have T R.
T → num {print (num.val);}
- Apply production rule 3, T → num {print (num.val);}, which expands T into num {print (num.val);}.
- num represents the first number in the input string, which is 9.
- {print (num.val);} is the action associated with T, which prints the value of num.
- So, the first number 9 is printed.
R → T {print ();} R
- Apply production rule 2, R → T {print ();} R, which expands R into T {print ();} R.
- T is the non-terminal symbol that represents a number, {print ();} is the action associated with T, and R is the non-terminal symbol that represents the remaining part of the input string.
- num represents the next number in the input string, which is 5.
- {print (num.val);} is the action associated with T, which prints the value of num.
- So, the second number 5 is printed.
R → T {print ();} R
- Apply production rule 2, R → T {print ();} R, which expands R into T {print ();} R.
- T is the non-terminal symbol that represents a number, {print ();} is the action associated with T, and R is the non-terminal symbol that represents the remaining part of the input string.
- num represents the last number in the input string, which is 2.
- {print (num.val);} is the action associated with T, which prints the value of num.
- So, the third number 2 is printed.
Therefore, the translation scheme will print the numbers in the input string as follows: 9 5 2.
Answer: Option B
To make sure you are not studying endlessly, EduRev has designed Computer Science Engineering (CSE) study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Computer Science Engineering (CSE).